import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    authority:true,
    isEmitAll: true,
    conInfo: {},
    loading: false,
    step: 0,
    form: {
      grade: [/* {
        "gradeId": 1,
        "gradeName": "2020"
      },
      {
        "gradeId": 2,
        "gradeName": "2021"
      } */],
      department: [
        /* {
          "departmentId": 1,
          "departmentName": "信息与机电工程学院"
        },
        {
          "departmentId": 2,
          "departmentName": "艺术学院"
        } */
      ],
      major: [
        /* {
          "majorId": 2,
          "majorName": "网络工程"
        },
        {
          "majorId": 1,
          "majorName": "通信工程"
        },
        {
          "majorId": 3,
          "majorName": "通信程"
        },
        {
          "majorId": 4,
          "majorName": "通工程"
        } */
      ],
      class: [
        /* {
          "classId": 2,
          "className": "20网工1班"
        },
        {
          "classId": 1,
          "className": "20通信1班"
        } */
      ]
    },
    userSelected: {
      grade: [],
      department: [],
      major: [],
      class: [],
      questionnaireType:0
    },
    badNetwork: false
  },
  getters: {
  },
  mutations: {
    authority(state,value){
      if(value === 'ROLE_SUPPER'){
        state.authority = true
      }else{
        state.authority = false
      }
    },
    changeEmitAll(state, value) {
      state.isEmitAll = value
    },
    loading(state, station) {
      state.loading = station
    },
    badNetwork(state, station) {
      state.badNetwork = station
    },
    nextStep(state, num) {
      console.log(num);
      state.step = num
    },
    setUserSelected(state, obj) {
      console.log(obj);
      if (state.isEmitAll && obj.arr === '全选') {     //如果全选并且选项为"全选"，则删除该条以确保不给后端传输该属性
        Vue.delete(state.userSelected, obj.key)
      } else {
        Vue.set(state.userSelected, obj.key, obj.arr)
      }
    },
    reset(state) {
      state.step = 0
      state.isEmitAll = true
      state.userSelected = {
        grade: [],
        department: [],
        major: [],
        class: []
      }
      state.form = {
        grade: [],
        department: [],
        major: [],
        class: []
      }
    },
    editGrade(state, arr) {
      state.form.grade = arr
    },
    editDepartment(state, arr) {
      state.form.department = arr
    },
    editMajor(state, arr) {
      state.form.major.push(arr[0])
    },
    editClass(state, arr) {
      state.form.class.push(arr[0])
    },
    conInfo(state, info) {
      state.conInfo = info
    }
  },
  actions: {
    getGrade(context) {
      context.commit('loading', true)
      axios({
        method: 'GET',
        url: '/quePublish/queryGrade',
        withCredentials: true,
        headers: {
          'Content-Type': 'application/json'
        }
      }).then((result) => {
        context.commit('editGrade', result.data)
        context.commit('loading', false)
        context.commit('badNetwork', false)

      }).catch((err) => {
        context.commit('badNetwork', true)
        context.commit('loading', false)
      });
    },
    getDepartment(context) {
      axios({
        method: 'GET',
        url: '/quePublish/queryDepartment',
        withCredentials: true,
        headers: {
          'Content-Type': 'application/json'
        }
      }).then((result) => {
        context.commit('editDepartment', result.data)
        context.commit('loading', false)
        context.commit('badNetwork', false)

      }).catch((err) => {
        context.commit('badNetwork', true)
        context.commit('loading', false)
      });
    },
    getMajor(context, departmentId) {
      context.commit('loading', true)

      axios({
        method: 'GET',
        url: '/quePublish/queryMajor',
        withCredentials: true,
        params: { departmentId },
        headers: {
          'Content-Type': 'application/json'
        }
      }).then((result) => {
        context.commit('editMajor', result.data)
        context.commit('loading', false)
        context.commit('badNetwork', false)

      }).catch((err) => {
        context.commit('badNetwork', true)
        context.commit('loading', false)
      });
    },
    getClass(context, obj) {
      context.commit('loading', true)

      axios({
        method: 'GET',
        url: '/quePublish/queryClass',
        withCredentials: true,
        params: { ...obj },
        headers: {
          'Content-Type': 'application/json'
        }
      }).then((result) => {
        context.commit('editClass', result.data)
        context.commit('loading', false)
        context.commit('badNetwork', false)

      }).catch((err) => {
        context.commit('badNetwork', true)
        context.commit('loading', false)
      });
    }
  },
  modules: {
  }
})
